//
//  WBBottomView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/13.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

class WBBottomView: UIView {
    
    private var relayButton : UIButton?
    private var commentButton : UIButton?
    private var praiseButton : UIButton?
    
    //外界赋值
    var statusViewModel: WBStatusViewModel?{
        
        didSet{
            
            //给按钮赋值
            //            - 底部视图的转发 评论 赞  业务逻辑
            //                - 如果 count <= 0  直接显示 转发 评论 赞
            //                    - 如果 count > 0 && count < 10000  是多少显示多少  示例  8888  8888
            //                        - 如果 count >= 10000   示例 12000   显示  1.2万
            //                            - 如果 count%10000 == 0   示例 20000  显示  2万
            relayButton?.setTitle(statusViewModel?.repostsStr, forState: .Normal)
            commentButton?.setTitle(statusViewModel?.commentsStr, forState: .Normal)
            praiseButton?.setTitle(statusViewModel?.attitudesStr, forState: .Normal)
            
        }
        
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        //添加控件
        relayButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        praiseButton = addChildButton("timeline_icon_unlike", title: "赞")
        let line1 = addChildLineImageView()
        let line2 = addChildLineImageView()
        
        //设置约束
        relayButton!.snp_makeConstraints { (make) in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        line1.snp_makeConstraints { (make) in
            make.leading.equalTo(relayButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        commentButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(relayButton!.snp_trailing)
            make.width.equalTo(praiseButton!)
        }
        line2.snp_makeConstraints { (make) in
            make.leading.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        praiseButton!.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
    }
    
    //添加Btn
    private func addChildButton(imageName:String , title:String) -> UIButton {
        
        //创建Btn
        let btn = UIButton()
        // 背景图片
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        // 设置image
        btn.setImage(UIImage(named:imageName ), forState: .Normal)
        //设置文字
        btn.setTitle(title, forState: .Normal)
        //设置文字颜色
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        //添加到视图中
        addSubview(btn)
        
        //返回对象 设置约束
        return btn
    }
    
    private func addChildLineImageView() -> UIImageView{
        
        let imageView = UIImageView(imgName: "timeline_card_bottom_line")
        addSubview(imageView)
        return imageView
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
